<?php

namespace app\model;

use app\message;
use app\Request;
use app\stateData;
use think\facade\Log;
use think\Model;
use think\model\concern\SoftDelete;

class WorkPlan extends Model
{
    /**
     * 计划任务表
     */
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $createTime='create_date';

    public function getCreateUidAttr($value)
    {
        return [
            'id'=>$value,
            'text'=>(new User())->getIdNickName($value)?:''
        ];
    }
    public function getIsCompleteAttr($name): array
    {
        return [
            'id'=>$name,
            'text'=>stateData::$_planTasks[$name]
        ];
    }

    public function setCreateUidAttr($value,$data)
    {
        if (!preg_match("/^\d*$/",$value)){

            $value = (new User())->getIdNickName($value);
        }

        return $value?:$data['uid'];
    }

//    public function setAbutmentAttr($value)
//    {
//        if (!preg_match("/^\d*$/",$value)){
//
//            $value = (new User())->getNameId($value);
//        }
//        return $value?:0;
//    }

    public function getUMobileAttr($name): string
    {
        return $name == 0 ? '' : $name;
    }
    public function getAbutmentAttr($name)
    {
        return $name?:'';
    }

    public function addPlanModel($data)
    {
        return self::create($data);
    }

    /**
     * 获取个人数据
     * @param $uid
     * @param $type
     * @param $page
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUidTypeListModel($uid,$type,$where,$page,$pageNum):array
    {
        return $this->where (['create_uid'=>$uid,'type'=>$type])->whereLike ('custom_name','%'.$where.'%')->order('create_date DESC')->page($page,$pageNum)->select()->toArray ();
    }
    public function getUidTypeListCountModel($uid,$type,$where):int
    {
        return $this->where (['create_uid'=>$uid,'type'=>$type])->whereLike ('custom_name','%'.$where.'%')->count();
    }

    public function getIdFindModel($id)
    {
        $data = $this->where (['id'=>$id])->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }
    public function getIdDataModel($id){
        return $this->where (['id'=>$id])->select ()->toArray ();
    }

    /**
     * 获取时间区间数据
     * @param $randUid
     * @param $type
     * @param $where
     * @param $page
     * @param $dateStart
     * @param $dateEnd
     * @param $pageNum
     * @param $complete
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */

    public function getBetweenTimeTypeListModel($randUid, $type, $where, $page, $dateStart, $dateEnd,$pageNum,$complete):array
    {
        return $this->where (['type'=>$type])
            ->whereIn ('is_complete',$complete)
            ->whereIn ('create_uid',$randUid)
            ->whereLike ('custom_name','%'.$where.'%')
            ->order('create_date DESC')
            ->whereBetweenTime('plan_date', $dateStart,$dateEnd)
            ->page($page,$pageNum)->select()
            ->toArray ();
    }

    public function getBetweenTimeTypeCountModel($randUid, $type, $where, $dateStart, $dateEnd,$complete):int
    {
        return $this->where (['type'=>$type])
            ->whereIn ('is_complete',$complete)
            ->whereIn ('create_uid',$randUid)
            ->whereLike ('custom_name','%'.$where.'%')
            ->order('create_date DESC')
            ->whereBetweenTime('plan_date', $dateStart,$dateEnd)
            ->count();
    }


    /**
     * 查询当月数据
     * @param $randUid
     * @param $type
     * @param $where
     * @param $page
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMonthDataModel($randUid,$type,$where,$page,$date,$pageNum):array
    {
        return $this->where (['type'=>$type])
            ->whereIn ('create_uid',$randUid)
            ->whereLike ('custom_name','%'.$where.'%')
            ->order('create_date DESC')
            ->whereMonth('plan_date',$date)
            ->page($page,$pageNum)->select()
            ->toArray ();
    }

    /**
     * 查询某一天的数据
     * @param $randUid
     * @param $type
     * @param $where
     * @param $page
     * @param $date
     * @param $pageNum
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDayDataModel($randUid,$type,$where,$page,$date,$pageNum)
    {
        return $this->where (['type'=>$type])
            ->whereIn ('create_uid',$randUid)
            ->whereLike ('custom_name','%'.$where.'%')
            ->order('create_date DESC')
            ->whereDay('plan_date',$date)
            ->page($page,$pageNum)->select()
            ->toArray ();
    }

    /**
     * 分页汇总
     * @param $randUid
     * @param $type
     * @param $where
     * @param $date
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getTypeListCountModel($randUid,$type,$where,$date):int
    {
        return $this->where (['type'=>$type])
            ->whereIn ('create_uid',$randUid)
            ->whereMonth('plan_date',$date)
            ->whereLike ('custom_name','%'.$where.'%')
            ->count();
    }

    /**
     * 查询当月已完成  未完成数据
     * @param $randUid
     * @param $type
     * @param $where
     * @param $date
     * @param $is_complete
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getCompleteCount($randUid,$type,$start_date,$end_date,$is_complete): int
    {

        return $this->where (['type'=>$type])
            ->whereIn ('create_uid',$randUid)
            ->where("is_complete",'=',$is_complete)
//            ->whereLike ('custom_name','%'.$where.'%')
            ->order('create_date DESC')
            ->whereBetweenTime('plan_date', $start_date,$end_date)
            ->count ();

    }
    /**
     * 根据id修改
     * @param $data
     * @return \app\model\WorkPlan|bool
     */
    public function editDataModel($data)
    {
        $result = WorkPlan::find ($data['id']);

        foreach ($data as $key=>$item) {

            if (!empty($data[$key])){
                $result[$key] = $item;
            }
        }
        return $result->save();
    }

    /**
     * 根据id删除
     * @param $id
     * @return bool
     */
    public function delIdModel($id): bool
    {
        return self::destroy($id);
    }

    /**
     * 根据时间区间查询
     * @param $startDate
     * @param $endDate
     * @return \app\model\WorkPlan[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getDateBETData($startDate,$endDate,$id,$type)
    {
        return $this->where ('plan_date','between',[$startDate,$endDate])->where ('type','=',$type)->where('create_uid','in',$id)->order('create_date DESC')->select();
    }
    /**
     * 获取区间已完成数据
     * @param $startDate
     * @param $endDate
     * @param $id
     * @param $type
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getWcDateBETData($startDate,$endDate,$id,$type): int
    {
        return $this
            ->where ('plan_date','between',[$startDate,$endDate])
            ->where('create_uid','in',$id)
            ->where ('type','=',$type)
            ->where ('is_complete','=',1)
            ->count();
    }

    /**
     * 获取区间未完成数据
     * @param $startDate
     * @param $endDate
     * @param $id
     * @param $type
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function getWwcDateBETData($startDate,$endDate,$id,$type): int
    {
        return $this
            ->where ('plan_date','between',[$startDate,$endDate])
            ->where('create_uid','in',$id)
            ->where ('type','=',$type)
            ->where ('is_complete','=',2)
            ->count();
    }

    /**
     * 批量添加数据
     * @param $data
     * @param $uid //导入赋值当前用户登陆id
     * @return \think\Collection
     * @throws \Exception
     */
    public function addDataAll($data){
        return $this->saveAll ($data,true);
    }

    /**
     * 导出excel数据
     * @param $type
     * @param $randUid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function exportDataModel($type,$randUid,$custom_name,$start_date,$end_date,$complete)
    {
        return $this->where (['type'=>$type])
            ->whereIn ('is_complete',$complete)
            ->whereIn ('create_uid',$randUid)
            ->whereLike ('custom_name','%'.$custom_name.'%')
            ->whereBetweenTime('plan_date', $start_date,$end_date)
            ->order('create_date DESC')
            ->select()->toArray ();
    }

    public function getCompletewCount($startDate,$endDate,$uid)
    {
        return $this->where (['is_complete'=>2,'type'=>1])->whereIn ('create_uid',$uid)->whereBetweenTime ('plan_date',$startDate,$endDate)->count();
    }

    /**
     * 查询未完成、未提醒消息
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIsTxDataModel()
    {
        return $this->where (['is_tx'=>0,'is_complete'=>2])->select ()->toArray ();
    }

    /**
     * 设置已提醒
     * @param $id
     * @return \app\model\WorkPlan
     */
    public function setTxFindModel($id)
    {
        return $this->where (['id'=>$id])->update (['is_tx'=>1]);
    }
    /**
     * 通过id修改内容
     * @param $id
     * @param $data
     */
    public function esitIdFindModel($id,$data)
    {
        return $this->where (['id'=>$id])->update ($data);
    }
    public function delIdFindModel($execution_date,$uid,$bid,$is_complete)
    {
        $data =  $this->where (['create_uid'=>$uid,'b_id'=>$bid,'is_complete'=>$is_complete])->order('plan_date ASC')->whereTime('plan_date',$execution_date)->findOrEmpty();
        if (!$data->isEmpty ()){
            return self::destroy ($data['id']);
        }
        return false;
    }
    public function getWhereFindModel($next_communicate_date, $user_login_id, $id, int $int)
    {
        $data = $this->where (['create_uid'=>$user_login_id,'b_id'=>$id,'is_complete'=>$int])->order('plan_date ASC')->whereTime('plan_date',$next_communicate_date)->findOrEmpty ();

        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }
}